package com.ruoyi.project.im.service.impl;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.im.domain.ImMessage;
import com.ruoyi.project.im.mapper.ImMessageMapper;
import com.ruoyi.project.im.mapper.ImTemporaryMessageMapper;
import com.ruoyi.project.im.service.IImMessageService;
import com.ruoyi.project.im.vo.MessageVo;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysUserService;
import com.ruoyi.project.websocket.component.SocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 消息记录Service业务层处理
 *
 * @author hongwj
 * @date 2024-03-01
 */
@Service
public class ImMessageServiceImpl implements IImMessageService {
    @Autowired
    private ImMessageMapper imMessageMapper;

    @Autowired
    private SocketServer socketServer;

    @Autowired
    private ImTemporaryMessageMapper temporaryMessageMapper;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询消息记录
     *
     * @param id 消息记录主键
     * @return 消息记录
     */
    @Override
    public ImMessage selectImMessageById(String id) {
        return imMessageMapper.selectImMessageById(id);
    }

    /**
     * 查询消息记录列表
     *
     * @param imMessage 消息记录
     * @return 消息记录
     */
    @Override
    public List<ImMessage> selectImMessageList(ImMessage imMessage) {
        return imMessageMapper.selectImMessageList(imMessage);
    }

    /**
     * 新增消息记录
     *
     * @param imMessage 消息记录
     * @return 结果
     */
    @Override
    public int insertImMessage(ImMessage imMessage) {
        imMessage.setTimestamp(System.currentTimeMillis());
        int rows = imMessageMapper.insertImMessage(imMessage);
        socketServer.sendMessageByUserId(imMessage.getReceiverId(), JSON.toJSONString(imMessage));
        return rows;
    }

    /**
     * 修改消息记录
     *
     * @param imMessage 消息记录
     * @return 结果
     */
    @Override
    public int updateImMessage(ImMessage imMessage) {
        return imMessageMapper.updateImMessage(imMessage);
    }

    /**
     * 批量删除消息记录
     *
     * @param ids 需要删除的消息记录主键
     * @return 结果
     */
    @Override
    public int deleteImMessageByIds(String[] ids) {
        return imMessageMapper.deleteImMessageByIds(ids);
    }

    /**
     * 删除消息记录信息
     *
     * @param id 消息记录主键
     * @return 结果
     */
    @Override
    public int deleteImMessageById(String id) {
        return imMessageMapper.deleteImMessageById(id);
    }

    @Override
    public List<MessageVo> queryPrivateTemporaryMessage(Long currentUserId, Long chatUserId) {
        List<MessageVo> results = temporaryMessageMapper.queryPrivateTemporaryMessage(currentUserId, chatUserId);
        temporaryMessageMapper.deletePrivateTemporaryMessage(currentUserId,chatUserId);
        return results;
    }

    @Override
    public List<Map<String, Object>> queryChatUserMessage() {
        List<Map<String, Object>> maps = new ArrayList<>();
        List<Long> chatUserIds = imMessageMapper.queryChatUser(SecurityUtils.getUserId());
        if (chatUserIds.isEmpty()) {
            return maps;
        } else {
            for (Long chatUserId : chatUserIds) {
                SysUser user = userService.selectUserById(chatUserId);
                Map<String, Object> map = imMessageMapper.queryLastMessage(SecurityUtils.getUserId(), chatUserId);
                String type = map.get("type").toString();
                if (type.equals("text")) {
                    map.put("lastMessage", map.get("lastMessage").toString());
                }
                if (type.equals("image")) {
                    map.put("lastMessage", "[图片]");
                }
                if (type.equals("video")) {
                    map.put("lastMessage", "[视频]");
                }
                map.put("chatUserId", user.getUserId());
                map.put("avatar", user.getAvatar());
                map.put("username", user.getNickName());
                map.put("unread",temporaryMessageMapper.queryPrivateTemporaryMessage(SecurityUtils.getUserId(), chatUserId).size());
                maps.add(map);
            }
        }
        return maps;
    }
}
